package gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import client.Client;
import client.Clientfunc;
import ocsf.client.*;
import client.*;
import java.awt.Dimension;

 

public class MainPanel extends JFrame {

	final public static int DEFAULT_PORT = 5555;  
    private String host = ip;       
    int port=0;
	public static Client client1;
	public static String[] Userinfo;
	static String ip;
	
	private JPanel login = null;
	private JLabel jLabelUserName = null;
	private JLabel jLabelPassword = null;
	private JLabel jLabelBackground = null;
	private JLabel jLabelWelcome = null;
	private JTextField jTextFieldUserName = null;
	private JPasswordField jPasswordFieldPassword = null;
	private JButton jButtonOK = null;
	private MainCustomer MC;	
	private BufferedImage image;
	private JLabel jLabelIP = null;
	private JTextField jTextFieldIp = null;
	
	public MainPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes MainPanel 
	 */
	private void initialize() {
		this.setSize(1000, 718);
		this.setContentPane(getlogin());
		this.setTitle("Flower Shop");		
	}
/**
 * this method initializes getlogin panel
 * @return login
 */
	public JPanel getlogin() {
		if (login == null) {				
			jLabelIP = new JLabel();
			jLabelIP.setBounds(new Rectangle(375, 431, 96, 32));
			jLabelIP.setText("IP ADDRESS:");
			ImageIcon imageBack = new ImageIcon("3.jpg"); 					
			ImageIcon welcome = new ImageIcon("welcome-logo-spring_1_.png");			
			jLabelBackground = new JLabel();
			jLabelBackground.setIcon(imageBack);
			jLabelBackground.setBounds(0, 0, 992, 681);
			jLabelWelcome = new JLabel();
			jLabelWelcome.setIcon(welcome);
			jLabelWelcome.setBounds(373, 14, 330, 330);
			jLabelPassword = new JLabel();
			jLabelPassword.setBounds(new Rectangle(375, 396, 96, 32));
			jLabelPassword.setText("PASSWORD");		
			jLabelUserName = new JLabel();
			jLabelUserName.setText("USERNAME");
			jLabelUserName.setBounds(new Rectangle(374, 360, 96, 32));
			login = new JPanel();
			login.setLayout(null);				
			login.add(jLabelUserName, null);
			login.add(jLabelPassword, null);
			login.add(getJTextFieldUserName(), null);
			login.add(getJPasswordFieldPassword(), null);
			login.add(getJButtonOK(), null);			
			login.add(jLabelWelcome, null);
			login.add(jLabelIP, null);
			login.add(getJTextFieldIp(), null);
			login.add(jLabelBackground, null);
			
			
			
			

		}
		return login; 
	}

	/**
	 * This method initializes jTextFieldUserName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldUserName() {
		if (jTextFieldUserName == null) {
			jTextFieldUserName = new JTextField();
			jTextFieldUserName.setBounds(new Rectangle(484, 362, 166, 29));
			
		}
		return jTextFieldUserName;
	}

	/**
	 * This method initializes jPasswordFieldPassword	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordFieldPassword() {
		if (jPasswordFieldPassword == null) {
			jPasswordFieldPassword = new JPasswordField();
			jPasswordFieldPassword.setBounds(new Rectangle(484, 397, 166, 29));
		}
		return jPasswordFieldPassword;
	}
	/**
	 * This method initializes jTextFieldIp	
	 * 	
	 * @return javax.swing.jTextFieldIp	
	 */
	private JTextField getJTextFieldIp() {
		if (jTextFieldIp == null) {
			jTextFieldIp = new JTextField();
			jTextFieldIp.setBounds(new Rectangle(484, 431, 166, 29));
		}
		return jTextFieldIp;
	}
	/**
	 * This method initializes jButtonOK, also it takes users information and sends them to the server and gets the users information from the database	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setBounds(new Rectangle(471, 547, 77, 46));
			jButtonOK.setText("OK");
			jButtonOK.setBackground(getBackground());
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {				
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					
					String[] User = new String[5];
					User[0] = "login";						
					User[1] = jTextFieldUserName.getText();													
					User[2] = jPasswordFieldPassword.getText();
					ip =jTextFieldIp.getText();
					if (!(ip.equals(null)))
						host=ip;
					else
						host="localhost";
						Client client1 = new Client(host,DEFAULT_PORT);
							if (User[1].equalsIgnoreCase("") || User[2].equalsIgnoreCase(""))
							{								
								ErrorFrame EFrame = new ErrorFrame();
								EFrame.jLabelMassage.setText("PLEASE INSERT USERNAME AND PASSWORD");
								EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								EFrame.setSize( 380,217 ); // set frame size
								EFrame.setResizable(true);
								EFrame.setVisible( true ); 															
							}
							else
							{		
								client1.accept(User);
								Object p = client1.getMsg();
								Userinfo = (String[])p;
								
							if (Userinfo[1].equals("1"))
							{	
								ErrorFrame EFrame = new ErrorFrame();
								EFrame.jLabelMassage.setText("INCORRECT PASSWORD, PLEASE TRY AGAIN!");
								EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								EFrame.setSize( 380,217 ); // set frame size
								EFrame.setResizable(true);
								EFrame.setVisible( true ); 
								jPasswordFieldPassword.setText(null);
								
							}
							if (Userinfo[1].equals("2"))
							{
								ErrorFrame EFrame = new ErrorFrame();
								EFrame.jLabelMassage.setText("USER DOES NOT EXIST! PLEASE CONTACT SYSTEM MANAGER");
								EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								EFrame.setSize( 450,217 ); // set frame size
								EFrame.setResizable(true);
								EFrame.setVisible( true ); 
								jTextFieldUserName.setText(null);
								jPasswordFieldPassword.setText(null);
								jTextFieldUserName.requestFocus();
							}
							if (Userinfo[1].equals("3"))
							{
								ErrorFrame EFrame = new ErrorFrame();
								EFrame.jLabelMassage.setText("THE USER IS ALREADY LOGGED IN");
								EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								EFrame.setSize( 380,217 ); // set frame size
								EFrame.setResizable(true);
								EFrame.setVisible( true ); 
								jTextFieldUserName.setText(null);
								jPasswordFieldPassword.setText(null);
								jTextFieldUserName.requestFocus();
							}
							
							if (Userinfo[1].equals("4"))
							{		
								
								if (Userinfo[2].equals("1"))
								{
									MainCustomer MFrame = new MainCustomer();
									MFrame.jTextFieldloginInfo.setText(Userinfo[5]);
								    MFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								    MFrame.setSize( 1000,720 ); // set frame size
								    MFrame.setResizable(true);
								    MFrame.setVisible( true ); // display frame								    
								    Client.aFrame.dispose();
								}
								if (Userinfo[2].equals("2"))
								{
									MainEmployee MFrame = new MainEmployee();
									MFrame.jTextFieldloginInfo.setText(Userinfo[5]);
									MFrame.jButtonReports.setVisible(false);
								    MFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								    MFrame.setSize(1000,720 ); // set frame size
								    MFrame.setResizable(true);
								    MFrame.setVisible( true ); // display frame	
								    
								    Client.aFrame.dispose();	
								}
								if ((Userinfo[2].equals("4")) || (Userinfo[2].equals("5")))
								{
									MainEmployee MFrame = new MainEmployee();
									MFrame.jTextFieldloginInfo.setText(Userinfo[5]);
								//	MFrame.jButtonGoToComplaints.setVisible(false);
								//	MFrame.jTextFieldComplaintsNum.setVisible(false);
								//	MFrame.jLabelComplaints.setVisible(false);
									MFrame.jButtonQuestion.setVisible(false);
								    MFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								    MFrame.setSize( 1000,720 ); // set frame size
								    MFrame.setResizable(true);
								    MFrame.setVisible( true ); // display frame								    
								    Client.aFrame.dispose();	
								}
								if ((Userinfo[2].equals("3")))
								{
									MainSystemManger MFrame = new MainSystemManger();
									MFrame.jTextFieldloginInfo.setText(Userinfo[5]);
								    MFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								    MFrame.setSize( 1000,720 ); // set frame size
								    MFrame.setResizable(true);
								    MFrame.setVisible( true ); // display frame								    
								    Client.aFrame.dispose();	
								}
							}
					
							
				
							}//else
				}
				
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jTextFieldIp	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	
	
}  //  @jve:decl-index=0:visual-constraint="12,11"
